package com.ruoyi.web.hnsdsa.util;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

public class CertNumberUtil {

    public static void main(String[] args) {

        int re = confirmCertNumber("郭震", "41070319920826203x");

        System.out.println(
              re
        );
    }

    /**
     * 传入姓名和身份证号
     * @param name
     * @param certNumber
     * @return  1 正常 0 错误
     */
    public static int confirmCertNumber(String name, String certNumber) {
        String key = "fc4e743b29d0d87b820904c166bbe917";
        String url = "http://op.juhe.cn/idcard/query?key=" +
                key +
                "&idcard=" + certNumber +
                "&realname=" + name;
        String result = HttpRequest.get(url)
                .timeout(20000)
                .execute().body();


        JSONObject jsonObject = JSONUtil.parseObj(result);
        System.out.println(result);

        Object res = jsonObject.get("result");
        Object error_code = jsonObject.get("error_code");
        int i1 = Integer.parseInt(error_code.toString());
        if (i1 != 0) {
            return 9;
        }
        JSONObject jsonObject2 = JSONUtil.parseObj(res);

        Object res1 = jsonObject2.get("res");

//        System.out.println(res1);
        int i = Integer.parseInt(res1.toString());
        if (i == 1) {
            return 1;
        } else if (i == 2) {
            return 2;

        }

        return 0;
    }


    /**
     * 检查是否实名
     * @param name
     * @param certNumber
     * @return
     */
    public  static  Boolean certCheck(String name,String certNumber){
        int i = confirmCertNumber(name, certNumber);

        if (i==1){
            return true;
        }else{
            return false;

        }
    }
}
